$(function(){
	//请求数据
	createDetailTable({});

	//搜索确认按钮
	$("#confirmBtnD").click(function(){
		var data = {
			beginDate : $('#selectorBeginDateShow').html(),
			endDate : $('#selectorEndDateShow').html(),
			qryType: $('#qryToTypeS').val(),
			qryInOut: $('#selectorClearShowD').attr('data-value'), //清算状态
			qryCustStr: $('#qryCustStrS').val(), //凭据号
			qrySubject: $('#qrySubjectS').val(), //主体名称
			qryWare: $('#qryWareS').val() //单元名称
		}
		createDetailTable(data); //请求数据
		mui.back(); //mui 返回
	});


	//选择器
	$(".myPicker").click(function(){
		var self = this;
		var nowDate = new Date();
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDateD':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShowD").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDateD':
				datePicker.show(function(items) {
					$("#selectorEndDateShowD").html(items.value);
					datePicker.dispose();
				});
				break;
			//进出码
			case 'selectorClearD':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '入库'},{value: '0' ,text: '出库'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorClearShowD").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});
})

//获取往来明细数据
function createDetailTable(params){
	params = params || {};
	var detailParams = JSON.parse(window.localStorage.resourceParams);
	//显示当前查询条件
	$("#qryTypeD").html(params.qryType || '');
	$("#qryInOutD").html($('#selectorClearShowD').html());
	$("#beginDateD").html(params.beginDate || '');
	$("#endDateD").html(params.endDate || '');

	$('#table').html("");
	layui.use('table', function() {
		var table = layui.table;
		table.render({
			elem: '#table'
			, limit: 20
			// ,skin: 'line'
			, even: true
			, size: 'sm'
			, height: $(window).height() - $('.head-search').height() - 120
			, url: url1 + Utils.Api.rptHbzjDetail //数据接口
			, where: {
				accessid: window.localStorage.accessid || '',
				field: 'kcMainDate',
				direction: 'DESC',
				kcAccountId: detailParams.kcAccountId,
				kcMxUnitId: detailParams.kcMxUnitId,
				kcMxResId: detailParams.kcMxResId,
				kcMxStateId: detailParams.kcMxStateId,
				qryInOut: params.qryInOut || '',  //进出码
				qryType: params.qryType || '',//单据类型
				qryDateB: params.beginDate || '',
				qryDateE: params.endDate || '',
				qryCustStr: params.qryCustStr || '', //凭据号
				qrySubject: params.qrySubject || '', //主体名称
				qryWare: params.qryWare || '' //单元名称
			}
			, request: {
				pageName: 'pageIndex' //页码的参数名称，默认：page
				, limitName: 'limit' //每页数据量的参数名，默认：limit
			}
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: true //开启分页
			, sums: true
			, cols: [[ //表头
				{field: 'kcMainDate', title: '交易日期', align: 'center',width: 120, templet: function (d) {
					return !d.summary? dateTimeFormat("yyyy-MM-dd", new Date(d.kcMainDate)): '<span class="font-bold">' + d.summary+'</span>'
				}},
				{field: 'typeName', title: '单据类型', align: 'center', width: 100},
				{field: 'kcMainRemark', title: '摘要备注', align: 'center', width: 100},
				{field: 'kcInOut', title: '进出码', align: 'center', width: 100, templet: function(d){
					return !d.summary? d.kcInOut === 0? '出库': '入库' : ""
				}},
				{field: 'kcCostAmountSign', title: '金额', align: 'center', width: 100,  templet: function (d) {
					return !d.summary?  Utils.formatMoney(d.kcCostAmountSign):
						'<span class="font-bold">' +Utils.formatMoney(d.kcCostAmountSign)+'</span>'
				}},
				{field: 'kcMxFzAmountSign', title: '辅助金额', align: 'center', width: 100,templet: function (d) {
					return !d.summary?  Utils.formatMoney(d.kcMxFzAmountSign):
						'<span class="font-bold">' +Utils.formatMoney(d.kcMxFzAmountSign)+'</span>'
				}},
				{field: 'subjectNick', title: '主体名称', align: 'center', width: 100},
				{field: 'wareName', title: '单元名称', align: 'center', width: 100},
				{field: 'resName', title: '资源名称', align: 'center', width: 100},
				{field: 'stateName', title: '资源状态', align: 'center', width: 100},
				{field: 'kcMainNo', title: '进出单号', align: 'center', width: 100},
				{field: 'kcAccountId', title: '资金帐号', align: 'center', width: 100},
				{field: 'accountBank', title: '开户行', align: 'center', width: 100},
				{field: 'accountName', title: '开户名', align: 'center', width: 100},
				{field: 'accountAlias', title: '帐号简称', align: 'center', width: 100},
			]]
			,
		});
	});
}